package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.SinaPoi;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by david on 14-4-12.
 */
public class GetSinaPoiList extends BaseCommand {
    private String TAG = "GetSinaPoiList:";
    private String default_page = DataCenter.LISTVIEW_DEFAULT_PAGE + "";

    private String access_token = "2.00O2b7ECTKvEwD8507bbee2fHNrTqD"; // sina api token
    private String coordinate = "108.8989409,34.110707"; //维经度
    private String range = "5000"; //范围
    private String count = DataCenter.LISTVIEW_DEFAULT_COUNT + "";// 条数
    private String page = DataCenter.LISTVIEW_DEFAULT_PAGE + ""; // 当前页数

    public GetSinaPoiList() {
    }

    public GetSinaPoiList(String currentPage) {
        this.page = currentPage;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.SINA_OPEN_ADDRESS_INTERFECE;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("access_token",access_token);
        paramters.put("coordinate",coordinate);
        paramters.put("range",range);
        paramters.put("count",count);
        paramters.put("page",page);
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) {
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) {
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetSinaPoiList.yes()");
        try {
            Log.d(TAG, result.object.toString());
            JSONObject jsonObject = new JSONObject(result.object.toString());

            JSONArray jsonArray = jsonObject.optJSONArray("poilist");

            ArrayList<SinaPoi> targetList = DataCenter.sinaPoiList;
            if (default_page.equals(page)) {
                targetList.clear();
            }

            for(int i = 0; i < jsonArray.length(); ++i){
                JSONObject o = jsonArray.getJSONObject(i);
                SinaPoi sinaPoi = JsonUtil.jsonToObject(o.toString(), SinaPoi.class);
                targetList.add(sinaPoi);
            }

            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(DataCenter.LOG_ERROR, "GetSinaPoiList.yes(): JSON解析出错");
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetSinaPoiList.no()");
        Log.d(TAG, "GetSinaPoiList.no()");
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_FAILED);
    }
}
